<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Zabbix," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="前言&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;为什么需要做服务器jvm自动发现的监控呢？这个事情主要有两点原因：  zabbix默认监控jvm状态是使用jmx中转进行监控的，监控效率比较低下 zabbix使用jmx监控jvm的时候由于一个主机上的键值不能重复，也就导致了一台主机上只能监控一个jvm实例  &amp;#160;&amp;#160;&amp;#160;&amp;#">
<meta name="keywords" content="Zabbix">
<meta property="og:type" content="article">
<meta property="og:title" content="zabbix使用自动发现功能监控服务器各JVM进程状态">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/Zabbix/6. zabbix使用自动发现功能监控服务器各JVM进程状态/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="前言&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;为什么需要做服务器jvm自动发现的监控呢？这个事情主要有两点原因：  zabbix默认监控jvm状态是使用jmx中转进行监控的，监控效率比较低下 zabbix使用jmx监控jvm的时候由于一个主机上的键值不能重复，也就导致了一台主机上只能监控一个jvm实例  &amp;#160;&amp;#160;&amp;#160;&amp;#">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-02T17:37:59.331Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="zabbix使用自动发现功能监控服务器各JVM进程状态">
<meta name="twitter:description" content="前言&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;为什么需要做服务器jvm自动发现的监控呢？这个事情主要有两点原因：  zabbix默认监控jvm状态是使用jmx中转进行监控的，监控效率比较低下 zabbix使用jmx监控jvm的时候由于一个主机上的键值不能重复，也就导致了一台主机上只能监控一个jvm实例  &amp;#160;&amp;#160;&amp;#160;&amp;#">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/Zabbix/6. zabbix使用自动发现功能监控服务器各JVM进程状态/"/>





  <title>zabbix使用自动发现功能监控服务器各JVM进程状态 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/Zabbix/6. zabbix使用自动发现功能监控服务器各JVM进程状态/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">zabbix使用自动发现功能监控服务器各JVM进程状态</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:13:44+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为什么需要做服务器jvm自动发现的监控呢？这个事情主要有两点原因：</p>
<ol>
<li>zabbix默认监控jvm状态是使用jmx中转进行监控的，监控效率比较低下</li>
<li>zabbix使用jmx监控jvm的时候由于一个主机上的键值不能重复，也就导致了一台主机上只能监控一个jvm实例</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上两点原因导致zabbix通过jmx监控jvm的实现不是很理想，加上最近老大要求收集服务器上面跑的所有java应用的信息，于是自己琢磨了下，还是自己动手，丰衣足食。利用了周末的时间，通过使用shell脚本+java工具jstat+zabbix实现监控主机上多jvm实例的功能。</p>
<h2 id="一、概念的理解"><a href="#一、概念的理解" class="headerlink" title="一、概念的理解"></a>一、概念的理解</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，既然要监控jvm状态，那就必须要了解jvm里面的信息，楼主通过搜索资料加自动脑补，把网上的资料取其精华，去其糟粕，整理了一下。JVM中的内存分类分为堆内存和非堆内存，堆内存是给实际应用使用的，非堆内存是给jvm容器使用的。我们主要关心的是堆内存这块。在堆内存里面，给内存分为如下几块：</p>
<ol>
<li>Young代（年轻代）</li>
<li>Old代（老年代）</li>
<li>Perm代（永久代）（关于这一点，在JDK7和JDK8中情况不一样，将在后面进行分析）</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中，年轻代里面又分成了三块，如下：</p>
<ol>
<li>Eden代（伊甸园代）</li>
<li>survivor0代（0号幸存区）</li>
<li>survivor1代（1号幸存区）</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;至于更详细的关于JVM堆内存的信息，各位可以自行百度或者google，我这里就不赘述了，毕竟我也是个半桶水，自己找了点资料外加脑补到的一些东西，不敢在关公门前耍大刀了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然，还得科普一个东西，那就是GC，所谓的GC就是JVM在运行的时候会有一个垃圾回收机制，这个垃圾回收机制是什么情况呢？就是在程序运行的时候会产生很多已经不使用的空间，但还是被占用了的情况，这样会造成很多不必要的浪费，于是JVM就有一个垃圾回收机制，针对程序中已经不使用的内存资源，会进行回收释放，这个过程就叫做GC。当然，关于GC还有很多内容我这里也没有详述，理由同上条。各位看官只需要知道GC是JVM监控里面的一个很重要的参数就行了。</p>
<h2 id="二、JAVA工具的选用"><a href="#二、JAVA工具的选用" class="headerlink" title="二、JAVA工具的选用"></a>二、JAVA工具的选用</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;java工具有很多，关于jvm监控的工具主要有如下几个：</p>
<ul>
<li>jstat</li>
<li>jmap</li>
<li>jstack</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中jmap –heap pid可以抓出挺多的关于某个jvm的运行参数，但是老大提醒我最好不要使用jmap进行jvm监控，具体没有说明原因。于是本着打破砂锅问到底的精神，我又去搜了一把，发现了如下内容：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jmap最主要的危险操作是下面这三种： </p>
<ol>
<li>jmap -dump </li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个命令执行，JVM会将整个heap的信息dump写入到一个文件，heap如果比较大的话，就会导致这个过程比较耗时，并且执行的过程中为了保证dump的信息是可靠的，所以会暂停应用。</p>
<ol>
<li>jmap -permstat </li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个命令执行，JVM会去统计perm区的状况，这整个过程也会比较的耗时，并且同样也会暂停应用。</p>
<ol>
<li>jmap -histo:live </li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个命令执行，JVM会先触发gc，然后再统计信息。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的这三个操作都将对应用的执行产生影响，所以建议如果不是很有必要的话，不要去执行。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所以，从上面三点来看，jmap命令对jvm状态影响还是比较大的，而且执行jmap –heap的时间也比较长，效率较低，予以排除。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来是jstack，这个命令可以深入到JVM里面对JVM运行问题进行排查，据说还可以统计JVM里面的线程数量。但是这个命令执行效率也比较低，被排除掉了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;于是剩下的只有一个jstat命令了。下面来详细的讲解该命令的使用了，咳咳，各位快点打起点精神来，这可是重头戏来了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>首先，列出jstat命令的一些使用案例吧</strong>：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div></pre></td><td class="code"><pre><div class="line">============================================</div><div class="line">1.jstat -gc pid</div><div class="line">可以显示gc的信息，查看gc的次数，及时间。</div><div class="line">其中最后五项，分别是young gc的次数，young gc的时间，full gc的次数，full gc的时间，gc的总时间。</div><div class="line">S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT </div><div class="line">9792.0 10048.0 0.0 5143.2 242048.0 220095.4 323200.0 211509.3 186368.0 114451.6 317 4.850 4 0.971 5.821</div><div class="line">S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT </div><div class="line">1024.0 1024.0 0.0 320.0 11776.0 11604.6 260608.0 149759.6 39344.0 38142.6 4528.0 4303.1 5473 24.010 2 0.128 24.138</div><div class="line">2.jstat -gccapacity pid</div><div class="line">可以显示，VM内存中三代（young,old,perm）对象的使用和占用大小，</div><div class="line">如 PGCMN显示的是最小perm的内存使用量，PGCMX显示的是perm的内存最大使用量，</div><div class="line">PGC是当前新生成的perm内存占用量，PC是但前perm内存占用量。</div><div class="line">其他的可以根据这个类推， OC是old内纯的占用量。</div><div class="line">NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC PGCMN PGCMX PGC PC YGC FGC </div><div class="line">87360.0 262144.0 262144.0 9792.0 10048.0 242048.0 174784.0 786432.0 323200.0 323200.0 131072.0 262144.0 186368.0 186368.0 317 4</div><div class="line">NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC </div><div class="line">1536.0 174592.0 13312.0 512.0 512.0 11776.0 260608.0 349696.0 260608.0 260608.0 0.0 1083392.0 39344.0 0.0 1048576.0 4528.0 5474 2</div><div class="line">3.jstat -gcutil pid</div><div class="line">统计gc信息统计。</div><div class="line">S0 S1 E O P YGC YGCT FGC FGCT GCT </div><div class="line">0.00 51.19 83.29 65.44 61.41 317 4.850 4 0.971 5.821</div><div class="line"></div><div class="line">S0 S1 E O M CCS YGC YGCT FGC FGCT GCT </div><div class="line">68.75 0.00 46.74 57.47 96.95 95.03 5474 24.014 2 0.128 24.143</div><div class="line">4.jstat -gcnew pid</div><div class="line">年轻代对象的信息。</div><div class="line">S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT </div><div class="line">9792.0 10048.0 0.0 5143.2 3 15 9792.0 242048.0 198653.2 317 4.850</div><div class="line">S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT </div><div class="line">512.0 512.0 352.0 0.0 15 15 512.0 11776.0 8446.4 5474 24.014</div><div class="line">5.jstat -gcnewcapacity pid</div><div class="line">年轻代对象的信息及其占用量。</div><div class="line">NGCMN NGCMX NGC S0CMX S0C S1CMX S1C ECMX EC YGC FGC </div><div class="line">87360.0 262144.0 262144.0 87360.0 9792.0 87360.0 10048.0 262016.0 242048.0 317 4</div><div class="line">NGCMN NGCMX NGC S0CMX S0C S1CMX S1C ECMX EC YGC FGC </div><div class="line">1536.0 174592.0 13312.0 57856.0 512.0 57856.0 512.0 173568.0 11776.0 5475 2</div><div class="line">6.jstat -gcold pid</div><div class="line">old代对象的信息。</div><div class="line">PC PU OC OU YGC FGC FGCT GCT </div><div class="line">186368.0 114451.6 323200.0 211509.3 317 4 0.971 5.821</div><div class="line">MC MU CCSC CCSU OC OU YGC FGC FGCT GCT </div><div class="line">39344.0 38142.6 4528.0 4303.1 260608.0 149783.6 5475 2 0.128 24.148</div><div class="line">7.jstat -gcoldcapacity pid</div><div class="line">old代对象的信息及其占用量。</div><div class="line">OGCMN OGCMX OGC OC YGC FGC FGCT GCT </div><div class="line">174784.0 786432.0 323200.0 323200.0 317 4 0.971 5.821</div><div class="line">OGCMN OGCMX OGC OC YGC FGC FGCT GCT </div><div class="line">260608.0 349696.0 260608.0 260608.0 5475 2 0.128 24.148</div><div class="line"></div><div class="line"></div><div class="line">8.jstat -gcpermcapacity pid</div><div class="line">perm对象的信息及其占用量。</div><div class="line">PGCMN PGCMX PGC PC YGC FGC FGCT GCT </div><div class="line">131072.0 262144.0 186368.0 186368.0 317 4 0.971 5.821</div><div class="line">没有</div><div class="line">9.jstat -class pid</div><div class="line">显示加载class的数量，及所占空间等信息。</div><div class="line">Loaded Bytes Unloaded Bytes Time </div><div class="line">25315 45671.7 5976 7754.1 15.19</div><div class="line">Loaded Bytes Unloaded Bytes Time </div><div class="line">6472 11893.0 0 0.0 5.97</div><div class="line">10.jstat -compiler pid</div><div class="line">显示VM实时编译的数量等信息。</div><div class="line">Compiled Failed Invalid Time FailedType FailedMethod</div><div class="line">4219 3 0 63.36 1 org/aspectj/weaver/ResolvedType addAndRecurse</div><div class="line">Compiled Failed Invalid Time FailedType FailedMethod</div><div class="line">11364 1 0 107.53 1 sun/nio/cs/UTF_8<span class="variable">$Decoder</span> decode</div><div class="line">11.stat -printcompilation pid</div><div class="line">当前VM执行的信息。</div><div class="line">Compiled Size Type Method</div><div class="line">4219 2232 1 net/spy/memcached/protocol/ascii/BaseGetOpImpl initialize</div><div class="line">Compiled Size Type Method</div><div class="line">11364 212 1 com/alibaba/rocketmq/client/impl/consumer/RebalanceService run</div><div class="line">==================================================</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以看出上面我列出的命令执行结果为什么有两行呢，这是因为是用不同的jdk版本执行的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面是JDK7执行结果，下面是JDK8执行结果，这两个版本之间输出的结果是有差距的，下面，就来分析为什么会产生这种差异。</p>
<h3 id="JDK7和JDK8中JVM堆内存划分差异"><a href="#JDK7和JDK8中JVM堆内存划分差异" class="headerlink" title="JDK7和JDK8中JVM堆内存划分差异"></a>JDK7和JDK8中JVM堆内存划分差异</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果记性好的童鞋们应该还能记得我上面在介绍JVM堆内存分类的时候括号里写的那个东东吧，没错，就是这个东西导致的。在JDK7中的Perm代（永久代）在JDK8中被废除了，取而代之的是Metadata代（元数据代），据说这个元数据代相对于永久代进行了优化，如果不设置最大值的话，默认会按需增长， 不会造成像Perm代中内存占满后会爆出内存溢出的错误，元数据代也可以设置最大值，这样的话，当内存区域被消耗完的时候将会和Perm代一样爆出内存溢出的错误。（PS:原谅我的班门弄斧，只能解释到这一个层面了。）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好了，解释清楚了JDK7和JDK8的差异以后，接下来我们来解释jstat抓到的这些参数了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div></pre></td><td class="code"><pre><div class="line">jstat命令获取参数解析</div><div class="line">======================================================================================</div><div class="line">* S0C 年轻代中第一个survivor（幸存区）的容量 (字节)jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $1*1024&#125;'</span></div><div class="line">* S0U 年轻代中第一个survivor（幸存区）目前已使用空间 (字节)jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $3*1024&#125;'</span></div><div class="line">* S0 年轻代中第一个survivor（幸存区）已使用的占当前容量百分比jstat -gcutil <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $1&#125;'</span></div><div class="line">* S0CMX 年轻代中第一个survivor（幸存区）的最大容量 (字节)jstat -gcnewcapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $4*1024&#125;'</span></div><div class="line">* </div><div class="line">* S1C 年轻代中第二个survivor（幸存区）的容量 (字节)jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $2*1024&#125;'</span></div><div class="line">* S1U 年轻代中第二个survivor（幸存区）目前已使用空间 (字节)jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $4*1024&#125;'</span></div><div class="line">* S1 年轻代中第二个survivor（幸存区）已使用的占当前容量百分比jstat -gcutil <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $2&#125;'</span></div><div class="line">* S1CMX 年轻代中第二个survivor（幸存区）的最大容量 (字节)jstat -gcnewcapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $6*1024&#125;'</span></div><div class="line">* DSS 当前需要survivor（幸存区）的容量 (字节)（Eden区已满）jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $7*1024&#125;'</span></div><div class="line">* </div><div class="line">* EC 年轻代中Eden（伊甸园）的容量 (字节)jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $8*1024&#125;'</span></div><div class="line">* EU 年轻代中Eden（伊甸园）目前已使用空间 (字节)jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $9*1024&#125;'</span></div><div class="line">* ECMX 年轻代中Eden（伊甸园）的最大容量 (字节)jstat -gcnewcapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $8*1024&#125;'</span></div><div class="line">* E 年轻代中Eden（伊甸园）已使用的占当前容量百分比jstat -gcutil <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $3&#125;'</span></div><div class="line">* </div><div class="line">* NGCMN 年轻代(young)中初始化(最小)的大小 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $1*1024&#125;'</span></div><div class="line">* NGCMX 年轻代(young)的最大容量 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $2*1024&#125;'</span></div><div class="line">* NGC 年轻代(young)中当前的容量 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $3*1024&#125;'</span></div><div class="line">* </div><div class="line">* OC Old代的容量 (字节)jstat -gcold <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $3*1024&#125;'</span></div><div class="line">* OU Old代目前已使用空间 (字节)jstat -gcold <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $4*1024&#125;'</span></div><div class="line">* OGCMX old代的最大容量 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $8*1024&#125;'</span></div><div class="line">* OGCMN old代中初始化(最小)的大小 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $7*1024&#125;'</span></div><div class="line">* O old代已使用的占当前容量百分比jstat -gcutil <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $4&#125;'</span></div><div class="line">* OGC old代当前新生成的容量 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $9*1024&#125;'</span></div><div class="line">* </div><div class="line">* PC Perm(持久代)的容量 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $14*1024&#125;'</span></div><div class="line">* PU Perm(持久代)目前已使用空间 (字节)jstat -gc <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $10*1024&#125;'</span></div><div class="line">* PGCMX perm代的最大容量 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $12*1024&#125;'</span></div><div class="line">* PGCMN perm代中初始化(最小)的大小 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $11*1024&#125;'</span></div><div class="line">* P perm代已使用的占当前容量百分比 jstat -gcutil <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $5*1024&#125;'</span></div><div class="line">* PGC perm代当前新生成的容量 (字节)jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $13*1024&#125;'</span></div><div class="line">* </div><div class="line">* YGC 从应用程序启动到采样时年轻代中gc次数jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $15&#125;'</span></div><div class="line">* YGCT 从应用程序启动到采样时年轻代中gc所用时间(s)jstat -gcutil <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $7&#125;'</span></div><div class="line">* FGC从应用程序启动到采样时old代(全gc)gc次数jstat -gccapacity <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $16&#125;'</span></div><div class="line">* FGCT 从应用程序启动到采样时old代(全gc)gc所用时间(s)jstat -gcutil <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $9&#125;'</span></div><div class="line">* GCT 从应用程序启动到采样时gc用的总时间(s)jstat -gcutil <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $10&#125;'</span></div><div class="line">* </div><div class="line">* TT 持有次数限制jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $5&#125;'</span></div><div class="line">* MTT 最大持有次数限制jstat -gcnew <span class="variable">$pid</span>|tail -1|awk <span class="string">'&#123;print $6&#125;'</span></div><div class="line">*</div><div class="line">* Loadedjvm加载class数量</div><div class="line">* Unloadedjvm未加载class数量</div><div class="line">*</div><div class="line">* M元数据区使用比例</div><div class="line">* MC当前元数据空间大小</div><div class="line">* MU元数据空间使用大小</div><div class="line">* MCMN最小元数据容量 </div><div class="line">* MCMX最大元数据容量</div><div class="line">* </div><div class="line">* CCS压缩使用比例</div><div class="line">* CCSC当前压缩类空间大小</div><div class="line">* CCSU压缩类空间使用大小</div><div class="line">* CCSMN最小压缩类空间大小</div><div class="line">* CCSMX最大压缩类空间大小</div><div class="line">====================================================</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好了，上面就是我找到的一些对jstat获取的数据意思的统计，各位看官可以做个参考。<br>好了，这一章的内容到此基本结束，前面的东西都是一些理论类的东西，没有实际的操作。俗话说，光说不练假把式。接下来，我们将开启下一章的旅程，脚本+jstat的使用。</p>
<h2 id="三、脚本-jstat获取数据"><a href="#三、脚本-jstat获取数据" class="headerlink" title="三、脚本+jstat获取数据"></a>三、脚本+jstat获取数据</h2><h3 id="首先，我们来看一下该章节介绍的几个脚本吧："><a href="#首先，我们来看一下该章节介绍的几个脚本吧：" class="headerlink" title="首先，我们来看一下该章节介绍的几个脚本吧："></a>首先，我们来看一下该章节介绍的几个脚本吧：</h3><ol>
<li><p>jvm_list.sh 获取该机器上所有运行的JVM的进程对应的程序根目录以及程序名称</p>
</li>
<li><p>get_jvmlist.sh 将获取的该机器上的所有进程对应的程序名称序列化成json格式并发送给zabbix服务器</p>
</li>
<li><p>get_jvmstatus.sh 通过获取的程序根目录获取到对应的程序进程，再通过jstat抓取数据写入到文件中缓存</p>
</li>
<li><p>set_jvmstatus.sh zabbix通过调用该脚本获取缓存文件中的关于某个JVM进程的状态信息</p>
</li>
</ol>
<h3 id="简单介绍了上面几个脚本的功能，下面我们列出这几个脚本的实际内容："><a href="#简单介绍了上面几个脚本的功能，下面我们列出这几个脚本的实际内容：" class="headerlink" title="简单介绍了上面几个脚本的功能，下面我们列出这几个脚本的实际内容："></a>简单介绍了上面几个脚本的功能，下面我们列出这几个脚本的实际内容：</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">cat jvm_list.sh </div><div class="line"></div><div class="line"> <span class="comment">#!/bin/bash</span></div><div class="line">  </div><div class="line"> packagePath=/usr/<span class="built_in">local</span>/etc/scripts/package_path.txt</div><div class="line"> <span class="built_in">echo</span> -n &gt;<span class="variable">$packagePath</span></div><div class="line">  </div><div class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> `ps -fC java|tail -n +2|grep -v <span class="string">'flume'</span>|awk <span class="string">'&#123;print $2&#125;'</span>`;</div><div class="line"> <span class="keyword">do</span></div><div class="line"> pgrootpath=`ls -l /proc/<span class="variable">$i</span>/cwd|awk <span class="string">'&#123;print $NF&#125;'</span>`</div><div class="line"> <span class="keyword">if</span> [[ -r <span class="variable">$pgrootpath</span>/appconfig ]] &amp;&amp; [ `grep ^packagename= <span class="variable">$pgrootpath</span>/appconfig|wc -l`==1 ];<span class="keyword">then</span></div><div class="line"> packagename=$(grep ^packagename= <span class="variable">$pgrootpath</span>/appconfig 2&gt;/dev/null|awk -F<span class="string">'"'</span> <span class="string">'&#123;print $2&#125;'</span>)</div><div class="line"> <span class="keyword">elif</span> [[ -r <span class="variable">$pgrootpath</span>/webconfig ]] &amp;&amp; [ `grep ^packagename= <span class="variable">$pgrootpath</span>/webconfig|wc -l`==1 ];<span class="keyword">then</span></div><div class="line"> packagename=$(grep ^packagename= <span class="variable">$pgrootpath</span>/webconfig 2&gt;/dev/null|awk -F<span class="string">'"'</span> <span class="string">'&#123;print $2&#125;'</span>)</div><div class="line"> <span class="keyword">else</span></div><div class="line"> packagename=$(basename <span class="variable">$pgrootpath</span>)-1.0.0-bin.tar.gz</div><div class="line"> <span class="keyword">fi</span></div><div class="line">  </div><div class="line"> <span class="built_in">echo</span> <span class="string">"<span class="variable">$packagename</span> <span class="variable">$pgrootpath</span>"</span> &gt;&gt; <span class="variable">$packagePath</span></div><div class="line"> <span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该脚本的目的是先通过使用ps -fC java命令获取该机器上面除了flume进程外的所有其他java进程（我这边使用的是flume来收集业务日志的。）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后，通过获取到的PID使用ll /proc/pid/cwd命令获取该进程的程序根目录，后面那些判断是获取该进程对应的包名（这一步各位可以根据自己公司的情况自行修改，我这边取包名的方式并不能够匹配各位公司的设置，在下爱莫能助了。）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后是将获取到的程序根目录和包名存放在变量packagePath对应的文件中。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">cat get_jvmlist.sh </div><div class="line"> <span class="comment">#!/bin/bash</span></div><div class="line">  </div><div class="line"> TABLESPACE=`awk <span class="string">'&#123;print $1&#125;'</span> /usr/<span class="built_in">local</span>/etc/scripts/package_path.txt`</div><div class="line"> COUNT=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$TABLESPACE</span>"</span> |wc -l`</div><div class="line"> INDEX=0</div><div class="line"> <span class="built_in">echo</span> <span class="string">'&#123;"data":['</span></div><div class="line"> <span class="built_in">echo</span> <span class="string">"<span class="variable">$TABLESPACE</span>"</span> | <span class="keyword">while</span> <span class="built_in">read</span> LINE; <span class="keyword">do</span></div><div class="line"> <span class="built_in">echo</span> -n <span class="string">'&#123;"&#123;#TABLENAME&#125;":"'</span><span class="variable">$LINE</span><span class="string">'"&#125;'</span></div><div class="line"> INDEX=`expr <span class="variable">$INDEX</span> + 1`</div><div class="line"> <span class="keyword">if</span> [ <span class="variable">$INDEX</span> -lt <span class="variable">$COUNT</span> ]; <span class="keyword">then</span></div><div class="line"> <span class="built_in">echo</span> <span class="string">','</span></div><div class="line"> <span class="keyword">fi</span></div><div class="line"> <span class="keyword">done</span></div><div class="line"> <span class="built_in">echo</span> <span class="string">']&#125;'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个脚本的作用就是通过读取文件里面的包名，然后将包名进行json序列化输出，没什么好讲的，套路套一个循环脚本就行。<br>接下来就是重要的脚本了，调用jstat获取JVM状态，并缓存到文件中。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div></pre></td><td class="code"><pre><div class="line">cat get_jvmstatus.sh </div><div class="line"> <span class="comment">#!/bin/bash</span></div><div class="line">  </div><div class="line"> MAINCLASS=<span class="string">"*Main.class"</span></div><div class="line"> scriptPath=/usr/<span class="built_in">local</span>/etc/scripts</div><div class="line">  </div><div class="line"> cat <span class="variable">$scriptPath</span>/package_path.txt|<span class="keyword">while</span> <span class="built_in">read</span> line</div><div class="line"> <span class="keyword">do</span></div><div class="line"> packageName=$(<span class="built_in">echo</span> <span class="variable">$line</span>|awk <span class="string">'&#123;print $1&#125;'</span>)</div><div class="line"> pgRootPath=$(<span class="built_in">echo</span> <span class="variable">$line</span>|awk <span class="string">'&#123;print $2&#125;'</span>)</div><div class="line"> <span class="keyword">if</span> [[ -d <span class="variable">$pgRootPath</span>/tomcat ]];<span class="keyword">then</span></div><div class="line"> pid=$(cat <span class="variable">$pgRootPath</span>/tomcat/tomcat.pid)</div><div class="line"> <span class="keyword">else</span></div><div class="line"> mainPath=$(find <span class="variable">$pgRootPath</span> -name <span class="variable">$MAINCLASS</span>)</div><div class="line"> appName=$(<span class="built_in">echo</span> <span class="variable">$&#123;mainPath##*classes/&#125;</span>|sed <span class="string">'s#/#.#g'</span>|sed <span class="string">'s#.class##g'</span>)</div><div class="line"> pid=$(ps -fC java|grep <span class="string">"<span class="variable">$appName</span>"</span>|awk <span class="string">'&#123;print $2&#125;'</span>)</div><div class="line"> <span class="keyword">fi</span></div><div class="line"> javaHome=/usr/<span class="built_in">local</span>/java/jdk1.8.0</div><div class="line"> <span class="comment">#javaHome=/usr/local/java/latest</span></div><div class="line"> <span class="comment">#if [[ -r $pgRootPath/appconfig ]] &amp;&amp; [ `grep ^JAVA_HOME= $pgRootPath/appconfig|wc -l` == 1 ] &amp;&amp; [ `grep ^JAVA_HOME= $pgRootPath/appconfig|grep 8|wc -l` == 1 ];then</span></div><div class="line"> <span class="comment">#javaHome=$(grep ^JAVA_HOME= $pgRootPath/appconfig 2&gt;/dev/null|awk -F'=' '&#123;print $2&#125;')</span></div><div class="line"> <span class="comment">#javaHome=/usr/local/java/jdk1.8.0</span></div><div class="line"> <span class="comment">#else</span></div><div class="line"> <span class="comment"># if [[ -r $pgRootPath/webconfig ]] &amp;&amp; [ `grep ^'export JAVA_HOME=' $pgRootPath/webconfig|wc -l` == 1 ] &amp;&amp; [ `grep ^'export JAVA_HOME=' $pgRootPath/webconfig|grep 8|wc -l` == 1 ];then</span></div><div class="line"> <span class="comment"># #javaHome=$(grep ^'export JAVA_HOME=' $pgRootPath/webconfig 2&gt;/dev/null|awk -F'"' '&#123;print $2&#125;')</span></div><div class="line"> <span class="comment"># javaHome=/usr/local/java/jdk1.8.0</span></div><div class="line"> <span class="comment">#fi</span></div><div class="line"> <span class="comment">#fi</span></div><div class="line"> <span class="comment">#echo --------------------------------$pgRootPath</span></div><div class="line"> <span class="comment">#echo $javaHome</span></div><div class="line"> <span class="built_in">echo</span> -------------------------------<span class="variable">$pid</span></div><div class="line"> sleep 5</div><div class="line"> <span class="comment">#echo -n &gt;$scriptPath/package/$packageName</span></div><div class="line"> <span class="comment">#$javaHome/bin/jstat -gccapacity $pid &gt; ./package/$packageName 2&gt;/dev/null</span></div><div class="line"> <span class="comment">#$javaHome/bin/jmap -heap $pid&gt;&gt;./package/$packageName 2&gt;/dev/null</span></div><div class="line"> <span class="built_in">echo</span> gcnew &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="variable">$javaHome</span>/bin/jstat -gcnew <span class="variable">$pid</span> &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="built_in">echo</span> gcutil &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="variable">$javaHome</span>/bin/jstat -gcutil <span class="variable">$pid</span> &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="built_in">echo</span> gcnewcapacity &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="variable">$javaHome</span>/bin/jstat -gcnewcapacity <span class="variable">$pid</span> &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="built_in">echo</span> gccapacity &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="variable">$javaHome</span>/bin/jstat -gccapacity <span class="variable">$pid</span> &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="comment">#echo gcold &gt;&gt; $scriptPath/package/$packageName 2&gt;/dev/null</span></div><div class="line"> <span class="comment">#$javaHome/bin/jstat -gcold $pid &gt;&gt; $scriptPath/package/$packageName 2&gt;/dev/null</span></div><div class="line"> <span class="built_in">echo</span> gc &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="variable">$javaHome</span>/bin/jstat -gc <span class="variable">$pid</span> &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="built_in">echo</span> class &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="variable">$javaHome</span>/bin/jstat -class <span class="variable">$pid</span> &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="built_in">echo</span> cpu &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="built_in">echo</span> -e <span class="string">"CPU\n<span class="variable">$( ps aux|grep $pid|grep -v grep|awk '&#123;print $3&#125;')</span>"</span> &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="built_in">echo</span> mem &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line"> <span class="built_in">echo</span> -e <span class="string">"MEM\n<span class="variable">$( ps aux|grep $pid|grep -v grep|awk '&#123;print $6&#125;')</span>"</span> &gt;&gt; <span class="variable">$scriptPath</span>/package/<span class="variable">$packageName</span> 2&gt;/dev/null</div><div class="line">  </div><div class="line"> <span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里面首先是通过获取到程序的根目录，然后我这的java程序除了tomcat跑的之外，其他的java程序都是通过Main.class启动的，所以可以获取到AppName，这样通过ps命令就能找到其对应的PID了，而如果是tomcat启动的进程的话，在程序根目录下面的tomcat目录下有一个tomcat.pid文件里面有该程序的PID。后面被注释的那一端代码其实之前是加上去的，那段代码的作用是判断该进程使用的是JDK7还是JDK8启动的，当初的计划是想着如果是JDK7启动的进程就用JDK7的jstat去获取数据，如果是JDK8启动的进程就用JDK8的jstat去获取数据，后来发现不同版本的JDK获取的数据格式不同，于是。。。。。。后悔莫及的把那段代码注释掉了。后面综合公司实际情况考虑，JDK8的程序用得比较多，JDK7的程序相对来说比较少，并且慢慢都会向JDK8进行转换，所以，权衡利弊之下，之后将jstat的JDK全部换成了JDK8，这样的影响就是获取不到JDK7的永久代数据。当然，各位有兴趣的话，也可以JDK7和JDK8同时使用，在过滤输出文件的时候加一个标志位进行判断，当然，我这里暂时没有做这方面的修改。。。毕竟时间有限。。。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第四个脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div></pre></td><td class="code"><pre><div class="line">cat set_jvmstatus.sh </div><div class="line"> <span class="comment">#!/bin/bash</span></div><div class="line"> packageName=<span class="variable">$1</span></div><div class="line"> key=<span class="variable">$2</span></div><div class="line">  </div><div class="line"> <span class="keyword">if</span> [ <span class="variable">$2</span> == <span class="string">"S0C"</span> -o <span class="variable">$2</span> == <span class="string">"S0U"</span> -o <span class="variable">$2</span> == <span class="string">"S1C"</span> -o <span class="variable">$2</span> == <span class="string">"S1U"</span> -o <span class="variable">$2</span> == <span class="string">"DSS"</span> -o <span class="variable">$2</span> == <span class="string">"EC"</span> -o <span class="variable">$2</span> == <span class="string">"EU"</span> ];<span class="keyword">then</span></div><div class="line"> part=gcnew</div><div class="line"> <span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"S0"</span> -o <span class="variable">$2</span> == <span class="string">"S1"</span> -o <span class="variable">$2</span> == <span class="string">"E"</span> -o <span class="variable">$2</span> == <span class="string">"O"</span> -o <span class="variable">$2</span> == <span class="string">"M"</span> -o <span class="variable">$2</span> == <span class="string">"CCS"</span> -o <span class="variable">$2</span> == <span class="string">"YGCT"</span> -o <span class="variable">$2</span> == <span class="string">"FGCT"</span> -o <span class="variable">$2</span> == <span class="string">"GCT"</span> ];<span class="keyword">then</span></div><div class="line"> part=gcutil</div><div class="line"> <span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"S0CMX"</span> -o <span class="variable">$2</span> == <span class="string">"S1CMX"</span> -o <span class="variable">$2</span> == <span class="string">"ECMX"</span> ];<span class="keyword">then</span></div><div class="line"> part=gcnewcapacity</div><div class="line"> <span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"NGCMN"</span> -o <span class="variable">$2</span> == <span class="string">"NGCMX"</span> -o <span class="variable">$2</span> == <span class="string">"NGC"</span> -o <span class="variable">$2</span> == <span class="string">"OGCMX"</span> -o <span class="variable">$2</span> == <span class="string">"OGCMN"</span> -o <span class="variable">$2</span> == <span class="string">"OGC"</span> -o <span class="variable">$2</span> == <span class="string">"MCMN"</span> -o <span class="variable">$2</span> == <span class="string">"MCMX"</span> -o <span class="variable">$2</span> == <span class="string">"MC"</span> -o <span class="variable">$2</span> == <span class="string">"CCSMN"</span> -o <span class="variable">$2</span> == <span class="string">"CCSMX"</span> -o <span class="variable">$2</span> == <span class="string">"CCSC"</span> -o <span class="variable">$2</span> == <span class="string">"YGC"</span> -o <span class="variable">$2</span> == <span class="string">"FGC"</span> ];<span class="keyword">then</span></div><div class="line"> part=gccapacity</div><div class="line"> <span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"MU"</span> -o <span class="variable">$2</span> == <span class="string">"CCSU"</span> -o <span class="variable">$2</span> == <span class="string">"OC"</span> -o <span class="variable">$2</span> == <span class="string">"OU"</span> ];<span class="keyword">then</span></div><div class="line"> part=gc</div><div class="line"> <span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"Loaded"</span> -o <span class="variable">$2</span> == <span class="string">"Unloaded"</span> ];<span class="keyword">then</span></div><div class="line"> part=class</div><div class="line"> <span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"CPU"</span> ];<span class="keyword">then</span></div><div class="line"> part=cpu</div><div class="line"> <span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"MEM"</span> ];<span class="keyword">then</span></div><div class="line"> part=mem</div><div class="line"> <span class="keyword">else</span></div><div class="line"> <span class="built_in">echo</span> <span class="string">"Error input:"</span></div><div class="line"> <span class="built_in">exit</span> 0</div><div class="line"> <span class="keyword">fi</span></div><div class="line"> <span class="keyword">case</span> <span class="variable">$2</span> <span class="keyword">in</span></div><div class="line"> S0C)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $1*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> S0U)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $3*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> S0)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $0&#125;'</span></div><div class="line"> ;;</div><div class="line"> S0CMX)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $4*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> S1C)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $2*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> S1U)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $4*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> S1)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $2&#125;'</span></div><div class="line"> ;;</div><div class="line"> S1CMX)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $6*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> DSS)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $7*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> EC)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $8*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> EU)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $9*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> ECMX)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $8*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> E)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $3&#125;'</span></div><div class="line"> ;;</div><div class="line"> NGCMN)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $1*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> NGCMX)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $2*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> NGC)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $3*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> OC)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $7*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> OU)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $8*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> OGCMX)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $8*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> OGCMN)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $7*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> O)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $4&#125;'</span></div><div class="line"> ;;</div><div class="line"> OGC)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $9*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> M)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $5&#125;'</span></div><div class="line"> ;;</div><div class="line"> MC)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $13*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> MU)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $10*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> MCMN)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $11*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> MCMX)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $12*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> CCS)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $6&#125;'</span></div><div class="line"> ;;</div><div class="line"> CCSC)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $13*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> CCSU)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $12*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> CCSMN)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $14*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> CCSMX)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $15*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> YGC)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $17&#125;'</span></div><div class="line"> ;;</div><div class="line"> YGCT)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $8&#125;'</span></div><div class="line"> ;;</div><div class="line"> FGC)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $18&#125;'</span></div><div class="line"> ;;</div><div class="line"> FGCT)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $10&#125;'</span></div><div class="line"> ;;</div><div class="line"> GCT)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $11&#125;'</span></div><div class="line"> ;;</div><div class="line"> TT)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $5&#125;'</span></div><div class="line"> ;;</div><div class="line"> MTT)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $6&#125;'</span></div><div class="line"> ;;</div><div class="line"> Loaded)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $1&#125;'</span></div><div class="line"> ;;</div><div class="line"> Unloaded)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $3&#125;'</span></div><div class="line"> ;;</div><div class="line"> CPU)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%f\n", $1&#125;'</span></div><div class="line"> ;;</div><div class="line"> MEM)</div><div class="line"> grep -wA 2 ^<span class="string">"<span class="variable">$part</span>"</span> /usr/<span class="built_in">local</span>/etc/scripts/package/<span class="variable">$1</span>|tail -1|awk <span class="string">'&#123;printf "%d\n", $1*1024&#125;'</span></div><div class="line"> ;;</div><div class="line"> *)</div><div class="line"> <span class="built_in">echo</span> <span class="string">"Error input:"</span></div><div class="line"> ;;</div><div class="line"> <span class="keyword">esac</span></div><div class="line"> <span class="built_in">exit</span> 0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这套脚本没什么讲的，就是重复的进行一些判断，抓数据并输出（注意，之前写的获取的jstat参数的值其实是不准确的，获取的值是以KB为单位而不是以字节为单位，所以我取完数据后对数据进行成字节为单位了。）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来，讲一下这几个脚本该怎么部署。我这里的zabbix_agentd是通过yum安装的，所以安装在/usr/local目录下，配置文件在/usr/local/etc目录下，需要在zabbix_agentd.conf里面添加下面两行获取数据的key（注意，添加好后一定要记得重启zabbix_agentd进程）：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">UserParameter=jmx.discovery,/usr/<span class="built_in">local</span>/etc/scripts/get_jvmlist.sh</div><div class="line">UserParameter=jmx.resource[*],/usr/<span class="built_in">local</span>/etc/scripts/set_jvmstatus.sh <span class="variable">$1</span> <span class="variable">$2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后脚本都放置在/usr/local/etc/scripts/目录下，该目录下的脚本权限如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">-rwxr-xr-x 1 zabbix zabbix 326 3月 26 22:29 get_jvmlist.sh</div><div class="line"> -rwxr-xr-x 1 root root 2956 3月 28 20:57 get_jvmstatus.sh</div><div class="line"> -rwxr-xr-x 1 root root 818 3月 26 22:33 jvm_list.sh</div><div class="line"> drwxr-xr-x 2 zabbix zabbix 4096 3月 26 23:05 package</div><div class="line"> -rw-r--r-- 1 zabbix zabbix 1947 3月 29 11:23 package_path.txt</div><div class="line"> -rwxr-xr-x 1 zabbix zabbix 5240 3月 28 20:50 set_jvmstatus.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后需要在crontab里面定义jvm_list.sh和get_jvmstatus.sh脚本的定时任务，我这里定义的如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">* */1 * * * /usr/<span class="built_in">local</span>/etc/scripts/jvm_list.sh</div><div class="line">*/5 * * * * /usr/<span class="built_in">local</span>/etc/scripts/get_jvmstatus.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意这两个脚本必须要以root权限去执行，因为里面涉及到的一些命令只有root用户才有权限去执行。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后可以手动执行脚本去获取数据，看是否能够抓取到相应的数据。</p>
<h2 id="四、zabbix获取数据"><a href="#四、zabbix获取数据" class="headerlink" title="四、zabbix获取数据"></a>四、zabbix获取数据</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过之前的脚本部署，可以在zabbix_server上面通过zabbix_get命令去检查是否获取到了相应的数据：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">zabbix_get  -s xx.xx.xx.xx -k jmx.resource[Abcdefg-1.0.0-rc-bin.tar.gz,MEM]</div><div class="line">    641036288</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里可以获取到数据了(注意IP被注释掉了，为了保护隐私哈，包名也被刻意修改了)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来就可以部署模板了，至于模板已经做好了，可以直接在附件里面下载。至于模板我制作了一些简单的key的值收集，以及图像的展示，至于监控报警值的设置，由于各个公司的环境不一样，需要各位自己根据自己需求自行设置。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Zabbix/" rel="tag"># Zabbix</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Zabbix/5. zabbix2.2 使用自定义脚本监控网卡流量/" rel="next" title="zabbix2.2 使用自定义脚本监控网卡流量">
                <i class="fa fa-chevron-left"></i> zabbix2.2 使用自定义脚本监控网卡流量
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/Zabbix/7. Zabbix脚本报警介质自定义/" rel="prev" title="Zabbix脚本报警介质自定义">
                Zabbix脚本报警介质自定义 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一、概念的理解"><span class="nav-number">2.</span> <span class="nav-text">一、概念的理解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、JAVA工具的选用"><span class="nav-number">3.</span> <span class="nav-text">二、JAVA工具的选用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#JDK7和JDK8中JVM堆内存划分差异"><span class="nav-number">3.1.</span> <span class="nav-text">JDK7和JDK8中JVM堆内存划分差异</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、脚本-jstat获取数据"><span class="nav-number">4.</span> <span class="nav-text">三、脚本+jstat获取数据</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#首先，我们来看一下该章节介绍的几个脚本吧："><span class="nav-number">4.1.</span> <span class="nav-text">首先，我们来看一下该章节介绍的几个脚本吧：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#简单介绍了上面几个脚本的功能，下面我们列出这几个脚本的实际内容："><span class="nav-number">4.2.</span> <span class="nav-text">简单介绍了上面几个脚本的功能，下面我们列出这几个脚本的实际内容：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#四、zabbix获取数据"><span class="nav-number">5.</span> <span class="nav-text">四、zabbix获取数据</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
